#include "erl_interface.h"
#include "ei.h"

#ifdef MAC_OS_X
    #include <GDAL/gdal.h>
    #include <GDAL/cpl_string.h>
#else
    #include "gdal.h"
    #include "cpl_string.h"
#endif

#include "ai_image.h"

extern void free_band_data(band *pBand);
extern int spectrometer(char* file_name, byte* data, int size, band *pBand1, band *pBand2, band *pBand3);

ETERM *create_band_tuple(band *pBand);

int main() {
    GDALAllRegister();
        
    ETERM *tuplep, *intp;
    ETERM *fnp, *argp;
    int res = 0;
     byte buf[65536*127];
    long allocated, freed;

    erl_init(NULL, 0);

//FILE *fp = fopen("./out.log", "w");
    
    while (read_cmd(buf) > 0) {
        tuplep = erl_decode(buf);
        fnp = erl_element(1, tuplep);
        argp = erl_element(2, tuplep);
    
        if (strncmp(ERL_ATOM_PTR(fnp), "light_split", 11) == 0) {
            byte* data = ERL_BIN_PTR(argp);
            int size = ERL_BIN_SIZE(argp);
 //fprintf(fp, "size: %d\n", size);
 //fflush(fp);
            band band1, band2, band3;
            spectrometer("work.dat", data, size, &band1, &band2, &band3);
 //fprintf(fp, "prt: band1=%d, band2=%d, band3=%d\n", band1.data, band2.data, band3.data);
 //fflush(fp);

            int band1_size = band1.xSize * band1.ySize * sizeof(byte); 
            int band2_size = band2.xSize * band2.ySize * sizeof(byte); 
            int band3_size = band3.xSize * band3.ySize * sizeof(byte); 
//*
            ETERM *band_data[3];
            
            band_data[0] = create_band_tuple(&band1);
            band_data[1] = create_band_tuple(&band2);
            band_data[2] = create_band_tuple(&band3);
            
            ETERM *res_data;
            res_data = erl_mk_list(band_data, 3);
            int ss = erl_term_len(res_data);
//fprintf(fp, "term_len: %d\n", ss);
//fflush(fp);
            erl_encode(res_data, buf);
//int es = erl_ext_size(buf);
//fprintf(fp, "buf_size: %d\n", es);
//fflush(fp);            
            write_cmd(buf, ss);
            
            erl_free_compound(res_data);
//            erl_free_compound(band_data[0]);
//            erl_free_compound(band_data[1]);
//            erl_free_compound(band_data[2]);
            erl_free_array(band_data, 3);

            free_band_data(&band1);
            free_band_data(&band2);
            free_band_data(&band3);
 //fprintf(fp, "prt: band1=%d, band2=%d, band3=%d\n", band1.data, band2.data, band3.data);
 //fflush(fp);
        }
        else if (strncmp(ERL_ATOM_PTR(fnp), "foo", 3) == 0) {
            res = foo(ERL_INT_VALUE(argp));
            intp = erl_mk_int(res);
            erl_encode(intp, buf);
            write_cmd(buf, erl_term_len(intp));
        } 

        erl_free_compound(tuplep);
        erl_free_term(fnp);
        erl_free_term(argp);
        erl_free_term(intp);
    }
}
 
ETERM *create_band_tuple(band *pBand) {
    ETERM *band1_elems[3];
    band1_elems[0] = erl_mk_int(pBand->xSize);
    band1_elems[1] = erl_mk_int(pBand->ySize);
    band1_elems[2] = erl_mk_binary(pBand->data, pBand->xSize * pBand->ySize * sizeof(byte));
    return erl_mk_tuple(band1_elems, 3);
}

